<template>
	<view class="index-section">
		<tit tit="相关政策" url="/pages/home/swimIndex" v-if="home" />
		<view class="line"></view>
		<view class="main">
			<view class="item" v-for="(item,index) in list" :key="index" @tap.stop="handleDetail(item.id)">
				<view class="left">
					<image :src="$imageOss(item.image)" mode="aspectFill" style="" class="image"></image>
				</view>
				<view class="right">
					<view class="top">
						{{item.title}}
					</view>
					<view class="bot">
						<text class="time">{{item.createtime}}</text>
					</view>
				</view>
			</view>
			<view style="width: 100%; text-align: center; font-size: 28rpx; margin-top: 10rpx;">{{isText}}</view>
		</view>
	</view>
</template>

<script>
	import tit from '@/pages/index/cpns/title.vue';
	import {
		policyList,
		customDetail,
		policyDetail
	} from "@/request/homeApi.js"
	import brl from "@/request/baseUrl.js"
	export default {
		components: {
			tit,
		},
		props: {
			home: {
				type: Boolean,
				default: true
			},
			// 'list': {
			// 	default: [],
			// 	type: Array
			// },
			url: {
				default: '',
				type: String
			}
		},
		data() {
			return {
				list: [],
				isText: '',
				pdfUrl: ''
			};
		},
		mounted() {
			// this.getList()
		},
		methods: {
			getList() {
				let that = this
				policyList({
					page: 1,
					limit: 3,
					title: '',
					is_top: 2
				}).then(res => {
					that.list = res.data.list
					res.data.count == 0 ? that.isText = '暂无' : that.isText = ''
				})
			},
			handleDetail(id) {
				policyDetail({
					id
				}).then(res => {
					if (res.code == 200) {
						console.log(res.data,'sssssss')
						let arr = res.data.image ? res.data.image : {}
						if (arr != '' && arr != null) {
							this.pdfUrl = brl.baseUrl + arr
							setTimeout(() => {
								uni.getSystemInfo({
									success: res => {
										let that = this
										const spliceLength2 = arr.lastIndexOf("/");
										that.pathType = arr.slice(spliceLength2 + 1);
										console.log(that.pathType)
										const fileName = that.pathType
										
										wx.downloadFile({
											url: that.pdfUrl,
											filePath: `${wx.env.USER_DATA_PATH}/${fileName}`,
											success: res => {
												const filePath = res.filePath ||
													res.tempFilePath
												console.log(filePath)
												wx.openDocument({
													filePath: filePath,
													showMenu: true, // 显示右上角菜单
													fileType: 'pdf',
													success: () => {
														console.log(
															'打开文档成功'
															)
													},
													fail: err => {
														console.log(
															`打开文档失败`,
															err)
													}
												})
											},
											fail: err => {
												console.log(`下载文档失败`, err)
											}
										})
									}
								});
							}, 500)
						} else {
							uni.showToast({
								title: '暂无',
								duration: 1500,
								icon: 'none',
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							duration: 1500,
							icon: 'none',
						})
					}
				})
				
				// uni.navigateTo({
				// 	url: `/pages/home/festivalDetail?id=${id}&type=2`,
				// });
			},
		}
	}
</script>

<style lang="scss" scoped>
	.index-section {
		padding: 20rpx;
		background-color: #fff;

		.line {
			height: 2rpx;
			width: 100%;
			background-color: #DADADA;
			margin: 20rpx 0;
		}

		.main {
			display: flex;
			flex-direction: column;

			.item {
				display: flex;
				justify-content: space-between;
				margin: 16rpx 0;

				.left {
					width: 80rpx;
					height: 80rpx;
					margin-right: 20rpx;

					.image {
						border-radius: 50%;
						width: 100%;
						height: 100%;
					}
				}

				.right {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					font-size: 30rpx;

					.top {
						font-size: 28;
					}

					.bot {
						.time {
							color: #B6B6B6;
							font-family: PingFang SC;
							font-size: 24rpx;
							font-style: normal;
							font-weight: 400;
							margin-right: 24rpx;
							line-height: normal;
						}
					}
				}
			}
		}

	}
</style>